{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-25T09:19:28.518897Z",
     "start_time": "2025-03-25T09:19:26.140188Z"
    }
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'treeCreater'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mModuleNotFoundError\u001B[0m                       Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 5\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01msklearn\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmodel_selection\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m train_test_split\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mnumpy\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mnp\u001B[39;00m\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtreeCreater\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtreePlottter\u001B[39;00m\n\u001B[0;32m      7\u001B[0m iris \u001B[38;5;241m=\u001B[39m datasets\u001B[38;5;241m.\u001B[39mload_iris()\n",
      "\u001B[1;31mModuleNotFoundError\u001B[0m: No module named 'treeCreater'"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "import treeCreater\n",
    "iris = datasets.load_iris()\n",
    "X = pd.DataFrame(iris['data'], columns=iris['feature_names'])\n",
    "y = pd.Series(iris['target_names'][iris['target']])\n",
    " \n",
    " \n",
    "# 取三个样本为测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=15)\n",
    " \n",
    " \n",
    "# 剩下120个样本中，取30个作为剪枝时的验证集\n",
    "X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.25, random_state=15)\n",
    " \n",
    " \n",
    " \n",
    " \n",
    "# 不剪枝\n",
    "tree_no_pruning = treeCreater.DecisionTree('gini')\n",
    "tree_no_pruning.fit(X_train, y_train, X_val, y_val)\n",
    "print('不剪枝：', np.mean(tree_no_pruning.predict(X_test) == y_test))\n",
    " \n",
    " \n",
    "# 预剪枝\n",
    "tree_pre_pruning = treeCreater.DecisionTree('gini', 'pre_pruning')\n",
    "tree_pre_pruning.fit(X_train, y_train, X_val, y_val)\n",
    "print('预剪枝：', np.mean(tree_pre_pruning.predict(X_test) == y_test))\n",
    "# treePlottter.create_plot(tree_pre_pruning.tree_)\n",
    " \n",
    " \n",
    "# 后剪枝\n",
    "tree_post_pruning = treeCreater.DecisionTree('gini', 'post_pruning')\n",
    "tree_post_pruning.fit(X_train, y_train, X_val, y_val)\n",
    "print('后剪枝：', np.mean(tree_post_pruning.predict(X_test) == y_test))\n",
    "# treePlottter.create_plot(tree_post_pruning.tree_)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "5922d52c85b89ff3"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
